<div class="content-wrapper">
    <?php
    $form = $this->form;
    $form->prepare();

    ?>
    <?php echo $this->form()->openTag($form); ?>
    <section class="content-header">
        <?php echo $this->partial('layout/breadcrumb'); ?>

        <h1 class="pull-right" id="dbshop-menu-top">
            <a href="<?php echo $this->url('self-location');?>" class="btn btn-default btn-sm"><i class="fa fa-arrow-left"></i> <?php echo $this->translate('返回自提点列表'); ?></a>
            <button type="submit" class="btn btn-primary btn-sm"><i class="fa fa-check"></i> <?php echo $this->translate('保存自提点信息'); ?></button>
            <?php echo $this->HelpUrl('selfLocationAdd'); ?>
        </h1>
    </section>

    <section class="content">

        <div class="row">
            <div class="col-md-12">
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title"><?php echo ((isset($this->locationInfo) && $this->locationInfo->getLocationId()) ? $this->translate('编辑自提点') : $this->translate('添加自提点')); ?></h3>
                    </div>

                    <div class="box-body">
                        <div class="form-group">
                            <label class="col-sm-2 control-label"><?php echo $this->translate('自提点名称'); ?><span class="must_add_value">*</span></label>
                            <div class="col-sm-4">
                                <?php echo $this->formElement($form->get('locationName')); ?>
                                <?php echo $this->formElementErrors($form->get('locationName'), ['class'=>'error-message']); ?>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label"><?php echo $this->translate('地区'); ?></label>
                            <div class="col-sm-8" id="region">
                                <?php if(isset($this->locationInfo)) { ?>
                                    <span class="show_region_value"><?php echo $this->locationInfo->getRegionInfo();?></span>
                                    <button type="button" onclick="regionEdit();" class="btn btn-success btn-sm show_region_value"><i class="fa fa-pencil"></i> <?php echo $this->translate('修改'); ?></button>
                                <?php } ?>
                                <select id="show_address_area" name="show_address_area" class="form-control" style="display: inline;width: 22%;<?php echo isset($this->locationInfo) ? 'display: none' : '';?>">
                                    <option value=""><?php echo $this->translate('请选择'); ?></option>
                                    <?php if ($this->region and !empty($this->region)) {
                                        foreach ($this->region as $value) {
                                            ?>
                                            <option
                                                    value="<?php echo $value->getRegionId(); ?>"><?php echo $value->getRegionName(); ?></option>
                                            <?php
                                        }
                                    }
                                    ?>
                                </select>
                                <?php echo $this->formElement($form->get('regionId')); ?>
                                <?php echo $this->formElementErrors($form->get('regionId'), ['class'=>'error-message']); ?>
                                <?php echo $this->formElement($form->get('regionInfo')); ?>
                                <?php echo $this->formElementErrors($form->get('regionInfo'), ['class'=>'error-message']); ?>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label"><?php echo $this->translate('详细地址'); ?><span class="must_add_value">*</span></label>
                            <div class="col-sm-6">
                                <?php echo $this->formElement($form->get('locationAddress')); ?>
                                <?php echo $this->formElementErrors($form->get('locationAddress'), ['class'=>'error-message']); ?>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label"><?php echo $this->translate('自提点联系人'); ?><span class="must_add_value">*</span></label>
                            <div class="col-sm-3">
                                <?php echo $this->formElement($form->get('locationUser')); ?>
                                <?php echo $this->formElementErrors($form->get('locationUser'), ['class'=>'error-message']); ?>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label"><?php echo $this->translate('自提点电话'); ?><span class="must_add_value">*</span></label>
                            <div class="col-sm-3">
                                <?php echo $this->formElement($form->get('locationPhone')); ?>
                                <?php echo $this->formElementErrors($form->get('locationPhone'), ['class'=>'error-message']); ?>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label"><?php echo $this->translate('营业时间'); ?><span class="must_add_value">*</span></label>
                            <div class="col-sm-4">
                                <?php echo $this->formElement($form->get('locationBusinessHours')); ?>
                                <?php echo $this->formElementErrors($form->get('locationBusinessHours'), ['class'=>'error-message']); ?>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label"><?php echo $this->translate('排序'); ?><span class="must_add_value">*</span></label>
                            <div class="col-sm-2">
                                <?php echo $this->formElement($form->get('locationSort')); ?>
                                <?php echo $this->formElementErrors($form->get('locationSort'), ['class'=>'error-message']); ?>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label"><?php echo $this->translate('状态'); ?></label>
                            <div class="col-sm-4">
                                <div class="checkbox">
                                    <label>
                                        <?php echo $this->formElement($form->get('locationState')); ?><?php echo $this->translate('启用'); ?>
                                        <?php echo $this->formElementErrors($form->get('locationState'), ['class'=>'error-message']); ?>
                                    </label>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
        </div>

    </section>
    <?php echo $this->form()->closeTag(); ?>

</div>
<script>
    var SITE_REGION_URL = '<?php echo $this->url('region', ['action' => 'ajaxRegion']);?>';
    var AREA_SELECT_LANG = '<?php echo $this->translate('请选择'); ?>';
    regionInit("region");

    $(document).ready(function() {
        $("#<?php echo $form->getName(); ?>").validate({
            rules: {
                locationName: {
                    required: true
                },
                locationAddress: {
                    required: true
                },
                locationUser: {
                    required: true
                },
                locationPhone: {
                    required: true
                },
                locationBusinessHours: {
                    required: true
                },
                locationSort: {
                    required: true,
                    number: true,
                    min: 1
                }
            },
            messages: {
                locationName: {
                    required: "<?php echo $this->translate('自提点名称不能为空!'); ?>"
                },
                locationAddress: {
                    required: "<?php echo $this->translate('详细地址不能为空!'); ?>"
                },
                locationUser: {
                    required: "<?php echo $this->translate('自提点联系人不能为空!'); ?>"
                },
                locationPhone: {
                    required: "<?php echo $this->translate('自提点电话不能为空!'); ?>"
                },
                locationBusinessHours: {
                    required: "<?php echo $this->translate('自提点营业时间不能为空!'); ?>"
                },
                locationSort: {
                    required: "<?php echo $this->translate('排序不能为空!'); ?>",
                    number: "<?php echo $this->translate('排序必须为数字!'); ?>",
                    min: "<?php echo $this->translate('排序最小为1!'); ?>"
                }
            }
        });
    });

    /**
     * 暂未使用
     */
    function getLocationGeoCoding() {
        let locationInfo = $('#regionInfo').val()+$('#locationAddress').val();
        if(locationInfo.length > 0) {
            $.get("<?php echo $this->url('self-location', ['action' => 'ajaxLocationGeoCoding']); ?>", {locationAddress: locationInfo}, function (data) {
                if(data.status == 0) {
                    $('#locationGeoCoding').val(data.result.location.lng.toString()+','+data.result.location.lat.toString());
                } else shopMessage("<?php echo $this->translate('调用错误。'); ?>");
            }, 'json');
        } else shopMessage("<?php echo $this->translate('先选择地区并填写详细地址后，在进行点击。'); ?>");
    }
</script>
